//
//  OosLocationParser.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/23/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosLocationParser.h"


@implementation OosLocationParser

static OosLocationParser *instance;

+ (OosLocationParser *) instance
{
	if (!instance) {
		instance = [[OosLocationParser alloc] init];
	}
	return instance;
}

- (SluggedString *) parseSluggedString: (OosSluggedElement *) element
{
	if (!element) {
		return nil;
	}
	SluggedString *string = [SluggedString create];
	string.value = element.stringValue;
	string.slug = element.slug;
	return string;
}

- (OosLocation *) parseLocationFromService: (OosLocatedEntry *) entry
{
	OosLocation *location = [OosLocation create];
	
	location.address = [entry.address stringValue];
	location.locality = [self parseSluggedString: entry.locality];	
	location.dependentLocality = [self parseSluggedString: entry.dependentLocality];
	location.subDependentLocality = [self parseSluggedString: entry.subDependentLocality];
	location.administrativeArea = [self parseSluggedString: entry.administrativeArea];
	location.subAdministrativeArea = [self parseSluggedString: entry.subAdministrativeArea];
	location.country = [self parseSluggedString: entry.country];
    location.latitude = [NSNumber numberWithDouble:entry.where.latitude];
    location.longitude = [NSNumber numberWithDouble:entry.where.longitude];
    
	return location;
}

- (OosLocatedEntry *) reverseParseLocation: (OosLocation *) location {
    OosLocatedEntry* entry = [OosLocatedEntry entry];
    [self reverseParseLocation:location intoEntry:entry];
    return entry;
}

- (void) reverseParseLocation: (OosLocation *) location intoEntry:(OosLocatedEntry*) entry {
    if (![location isKindOfClass:[OosLocation class]]) {
        [NSException raise:@"Unsupported type" format:@"Expected object of class %@", [OosLocation className]];
    }
    if (![entry isKindOfClass:[OosLocatedEntry class]]) {
        [NSException raise:@"Unsupported type" format:@"Expected object of class %@", NSStringFromClass([OosLocatedEntry class])];
    }
    
    entry.address = [OosUserAddress valueWithString:location.address];
    entry.subDependentLocality = [OosSubDependentLocality oosSluggedElementWithString:location.subDependentLocality.value];
    entry.subDependentLocality.slug = location.subDependentLocality.slug;
    entry.dependentLocality = [OosDependentLocality oosSluggedElementWithString:location.dependentLocality.value];
    entry.dependentLocality.slug = location.dependentLocality.slug;
    entry.locality = [OosLocality oosSluggedElementWithString:location.locality.value];
    entry.locality.slug = location.locality.slug;
    entry.subAdministrativeArea = [OosSubAdministrativeArea oosSluggedElementWithString:location.subAdministrativeArea.value];
    entry.subAdministrativeArea.slug = location.subAdministrativeArea.slug;
    entry.administrativeArea = [OosAdministrativeArea oosSluggedElementWithString:location.administrativeArea.value];
    entry.administrativeArea.slug = location.administrativeArea.slug;
    entry.country = [OosCountry oosSluggedElementWithString:location.country.value];
    entry.country.slug = location.country.slug;
    entry.where = [GDataGeoRSSWhere geoWithLatitude:[location.latitude doubleValue]
                                          longitude:[location.longitude doubleValue]];
    
}

@end
